package org.jeecg.modules.device.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.DeviceConstant;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.device.entity.DevDeviceInfo;
import org.jeecg.modules.device.entity.DevDeviceRecord;
import org.jeecg.modules.device.mapper.DevDeviceInfoMapper;
import org.jeecg.modules.device.mapper.DevDeviceRecordMapper;
import org.jeecg.modules.device.service.IDevDeviceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Map;

/**
 * @Description: 设备记录表
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class DevDeviceRecordServiceImpl extends ServiceImpl<DevDeviceRecordMapper, DevDeviceRecord> implements IDevDeviceRecordService {

    @Autowired
    private DevDeviceRecordMapper deviceRecordMapper;

    @Autowired
    private DevDeviceInfoMapper deviceInfoMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public JSONObject getDeviceRecordById(String devId) {
        DevDeviceInfo devDeviceInfo = deviceInfoMapper.selectById(devId);
        return getDeviceRecordByCode(devDeviceInfo.getDeviceId());
    }

    @Override
    public JSONObject getDeviceRecordByCode(String deviceId) {
        // 当前租户
        String tenantId = oConvertUtils.getString(TenantContext.getTenant(), "0");
        Object obj = redisUtil.hget(DeviceConstant.DEVICE_REALTIME_DATA + "::" + tenantId, deviceId);
        if(null != obj){
            return (JSONObject)obj;
        }
        return null;
    }
}
